﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class frmXiuLi : Form
    {
        static string connectionString = ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);
        string tbx0, tbx1, tbx2, tbx3, tbx4, tbx5, tbx6, tbx7, tbx8, tbx9;
        string ptx1, ptx2, ptx3, ptx4, ptx5;
        string xiuli;
        bool b量具Search, b量具Insert, b量具Edit, b量具Del;
        public frmXiuLi(string xiuli, string tbx0, string tbx1, string tbx2, string tbx3, string tbx4, string tbx5, string tbx6, string ptx1, string ptx2, string ptx3, string ptx4, string ptx5, string tbx7, string tbx8, string tbx9,bool b量具Search,bool b量具Insert,bool b量具Edit,bool b量具Del)
        {
            InitializeComponent();
            this.tbx0 = tbx0;
            this.tbx1 = tbx1;
            this.tbx2 = tbx2;
            this.tbx3 = tbx3;
            this.tbx4 = tbx4;
            this.tbx5 = tbx5;
            this.tbx6 = tbx6;
            this.xiuli = xiuli;
            this.ptx1 = ptx1;
            this.ptx2 = ptx2;
            this.ptx3 = ptx3;
            this.ptx4 = ptx4;
            this.ptx5 = ptx5;
            this.tbx7 = tbx7;
            this.tbx8 = tbx8;
            this.tbx9 = tbx9;
            this.b量具Search = b量具Search;
            this.b量具Insert = b量具Insert;
            this.b量具Edit = b量具Edit;
            this.b量具Del = b量具Del;
        }

        private void frmXiuLi_Load(object sender, EventArgs e)
        {
            txt量具狀態.Enabled = false;
            txt量具狀態.Text = "修理";

            // goi du lieu lay san pham dung
            if (xiuli == "1")
            {
                btn_Save.Text = "存檔";
                label13.Text = "量具修理";
                txt_量具編號.Text = tbx1;//tb1 0
                txt_規格型式.Text = tbx2;//cb1 1
                txt_存放地點.Text = tbx3; //tb3 2
                txt_Name量具.Text = tbx4; //tb4 3
                txt_測量範圍.Text = tbx5; //cb2  4
            }
            // goi du lieu Tra san pham ve kho
            if (xiuli == "2")
            {
                label13.Text = "量具返庫";
                hiden();
                //txt_領用部門.Visible = false;
                //label10.Visible = false;
                //lbName領用部門.Visible = false;
                //btn請購人員Search.Visible = false;
                btn_Save.Text = "返庫";
                txt_量具編號.Text = tbx1;//tb1 0
                txt_規格型式.Text = tbx2;//cb1 1
                txt_存放地點.Text = tbx3; //tb3 2
                txt_Name量具.Text = tbx4; //tb4 3
                txt_測量範圍.Text = tbx5; //cb2  4
                txt數量.Text = "";
                txt_Date修理.Text = ptx1;
                txt_Date返庫.Text = ptx2;
                //txt_領用人.Text = ptx3;
                txt_修理人.Text = ptx4;
                txt_故障描述.Text = ptx5;
                txt_受理人.Text = tbx7;
                txt_修理內容.Text = tbx8;
                txt_note.Text = tbx9;
            }
            if (xiuli == "3")
            {
                label13.Text = "量具報廢轉修理";
                btn_Save.Text = "修理";
                txt_量具編號.Text = tbx1;//tb1 0
                txt_規格型式.Text = tbx2;//cb1 1
                txt_存放地點.Text = tbx3; //tb3 2
                txt_Name量具.Text = tbx4; //tb4 3
                txt_測量範圍.Text = tbx5; //cb2  4
                txt數量.Text = "";

                txt_量具編號.Enabled = false;
                txt_規格型式.Enabled = false;
                txt_存放地點.Enabled = false;
                txt_Name量具.Enabled = false;
                txt_測量範圍.Enabled = false;
            }
        }

        public void hiden()
        {
            btn修理紀錄.Enabled = false;
            txt_量具編號.Enabled = false;
            txt_規格型式.Enabled = false;
            txt_存放地點.Enabled = false;
            txt_Name量具.Enabled = false;
            txt_測量範圍.Enabled = false;
            txt數量.Enabled = true;
            txt_Date修理.Enabled = false;
            txt_Date返庫.Enabled = true;
            txt_修理人.Enabled = false;
            txt_故障描述.Enabled = true;
            txt_受理人.Enabled = true;
            txt_修理內容.Enabled = true;
            txt_note.Enabled = true;
        }

        public void Load修理()
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("sp_修理_load", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                //dataGV領用.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                //dataGV領用.DataSource = dt;
                //PublicClass.setRowNumber(dataGV領用);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                // lay san pham su dung
                if (xiuli == "1"){
                    if (txt_量具編號.Text == "")
                    {
                        MessageBox.Show("請您輸入量具編號", "錯誤", MessageBoxButtons.OK);
                        txt_量具編號.Focus();
                        return;
                    }
                    if (txt量具狀態.Text == "")
                    {
                        MessageBox.Show("請您輸入量具狀態", "錯誤", MessageBoxButtons.OK);
                        txt量具狀態.Focus();
                        return;
                    }
                    if (txt數量.Text == "")
                    {
                        MessageBox.Show("請您輸入修理數量", "錯誤", MessageBoxButtons.OK);
                        txt數量.Focus();
                        return;
                    }
                    if (txt_Date修理.Text == "")
                    {
                        MessageBox.Show("請您輸入修理日期", "錯誤", MessageBoxButtons.OK);
                        txt_Date修理.Focus();
                        return;
                    }
                    if (txt_Date返庫.Text == "")
                    {
                        MessageBox.Show("請您輸入返庫日期", "錯誤", MessageBoxButtons.OK);
                        txt_Date返庫.Focus();
                        return;
                    }

                    if (txt_修理人.Text == "")
                    {
                        MessageBox.Show("請您輸入修理人", "錯誤", MessageBoxButtons.OK);
                        txt_修理人.Focus();
                        return;
                    }

                    if (txt_受理人.Text == "")
                    {
                        MessageBox.Show("請您輸入受理人", "錯誤", MessageBoxButtons.OK);
                        txt_受理人.Focus();
                        return;
                    }
                    // kiem tra thoi gian
                    DateTime a = DateTime.Parse(txt_Date返庫.Text);
                    DateTime b = DateTime.Parse(txt_Date修理.Text);
                    if (a < b)
                    {
                        MessageBox.Show("請您輸入返庫日期 >= 修理日期", "錯誤", MessageBoxButtons.OK);
                        txt_Date返庫.Focus();
                        return;
                    }
                    // kiem tra nhap shuliang nho hon so luong trong kho
                    SqlCommand sqlcmd = new SqlCommand();
                    sqlcmd.Connection = con;
                    con.Open();
                    sqlcmd.CommandText = "select 数量 from 量具訊息 where 量具編號=" + "N'" + tbx1 + "'";
                    sqlcmd.CommandType = CommandType.Text;
                    var icount = sqlcmd.ExecuteScalar();
                    int inumdel;
                    bool k = int.TryParse(txt數量.Text, out inumdel);
                    int istocknumber = int.Parse(icount.ToString());
                    int check = int.Parse(txt數量.Text);
                    if (!k || inumdel > int.Parse(icount.ToString()))
                    {
                        MessageBox.Show("請您輸入修理數量量具要刪除(int,<" + istocknumber + ")", "錯誤", MessageBoxButtons.OK);
                        txt數量.Focus();
                        //return;
                    }
                    else if (istocknumber == 0 || check <=0)
                    {
                        MessageBox.Show("不能修理", "錯誤", MessageBoxButtons.OK);
                        }
                    else { 
                    // check bo phan               
                    //String sqlck = "SELECT COUNT(*) FROM 領用人,領用部門 WHERE 領用人代號='" + txt_領用人.Text.ToString()
                    //            + "' AND 領用人.領用部門代號=領用部門.領用部門代號 AND 領用部門.領用部門代號='" + txt_領用部門.Text.ToString() + "'";
                    //sqlcmd = new SqlCommand(sqlck, con);
                    //if ((int)sqlcmd.ExecuteScalar() == 0)
                    //{
                    //    MessageBox.Show("差失: 領用人代號= '" + txt_領用人.Text.ToString() + "' 没有出在 領用部門代號= " + txt_領用部門.Text.ToString(), "錯誤", MessageBoxButtons.OK);
                    //    txt_領用人.Focus();
                    //    //return;
                    //}
                    //else if ((int)sqlcmd.ExecuteScalar() != 0)
                    //{
                    // tien hanh inset du lieu vao bang lingyong
                    DialogResult answer = MessageBox.Show("您確定要出入" + int.Parse(txt數量.Text) + "量具嗎？", "警報", MessageBoxButtons.YesNo);
                    if (answer == DialogResult.Yes)
                    {
                        //automation inset
                        string sp_insert = "sp_修理_新增";
                        SqlCommand cmd = new SqlCommand(sp_insert, con);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@量具編號", txt_量具編號.Text);
                        cmd.Parameters.AddWithValue("@量具狀態", txt量具狀態.Text);
                        cmd.Parameters.AddWithValue("@量具名稱", txt_Name量具.Text);
                        cmd.Parameters.AddWithValue("@規格型號", txt_規格型式.Text);
                        cmd.Parameters.AddWithValue("@存放地點", txt_存放地點.Text.ToString());
                        cmd.Parameters.AddWithValue("@測量範圍", txt_測量範圍.Text.ToString());
                        cmd.Parameters.AddWithValue("@數量", Int32.Parse(txt數量.Text));
                        cmd.Parameters.AddWithValue("@修理日期", DateTime.Parse(txt_Date修理.Text));
                        cmd.Parameters.AddWithValue("@返庫日期", DateTime.Parse(txt_Date返庫.Text));
                        //cmd.Parameters.AddWithValue("@領用人代號", txt_領用部門.Text);
                        cmd.Parameters.AddWithValue("@修理人代號", txt_修理人.Text);
                        cmd.Parameters.AddWithValue("@故障描述", txt_故障描述.Text.ToString());
                        cmd.Parameters.AddWithValue("@受理人代號", txt_受理人.Text.ToString());                        
                        cmd.Parameters.AddWithValue("@修理內容", txt_修理內容.Text.ToString());
                        cmd.Parameters.AddWithValue("@備註", txt_note.Text);
                        cmd.ExecuteNonQuery();

                        //ling yong liang ju
                        string delete = tbx1;
                        //string delete1 = tbl6;
                        string sp_increment = "sp_量具訊息_更新_-n";// tru so luong san pham trong kho khi linh yong
                        SqlCommand sqlincrement = new SqlCommand();
                        sqlincrement.Connection = con;
                        sqlincrement.CommandType = CommandType.StoredProcedure;
                        sqlincrement.CommandText = sp_increment;
                        sqlincrement.Parameters.AddWithValue("@量具編號", delete);
                        //sqlincrement.Parameters.AddWithValue("@量具狀態", delete1);
                        sqlincrement.Parameters.AddWithValue("@n", int.Parse(txt數量.Text));
                        sqlincrement.ExecuteNonQuery();

                        //string dele = "Select 數量 from 領用";

                        MessageBox.Show("修理成功");
                        con.Close();
                        this.Close();
                        frmxiulijilu frm = new frmxiulijilu(b量具Search, b量具Insert, b量具Edit, b量具Del);
                        frm.ShowDialog();
                        this.Close();
                    }
                    else return;
                    }

                }
                if (xiuli == "2"){                    
                    //==============================================================================
                    if (txt數量.Text == "")
                    {
                        MessageBox.Show("請您輸入送檢數量", "錯誤", MessageBoxButtons.OK);
                        txt數量.Focus();
                        return;
                    }
                    int check = int.Parse(txt數量.Text);
                    if (check <= 0)
                    {
                        MessageBox.Show("不能返庫數量<=0", "錯誤", MessageBoxButtons.OK);
                        txt數量.Focus();
                        return;
                    }
                    else
                    {
                        // kiem tra so luong tra nho hon so luong ling yong
                        SqlCommand sqlcmd = new SqlCommand();
                        sqlcmd.Connection = con;
                        con.Open();
                        sqlcmd.CommandText = "select 數量 from 修理 where 項次=" + "N'" + tbx0 + "'";
                        sqlcmd.CommandType = CommandType.Text;
                        var icount = sqlcmd.ExecuteScalar();
                        int inumdel;
                        bool k = int.TryParse(txt數量.Text, out inumdel);
                        int istocknumber = int.Parse(icount.ToString());
                        if (!k || inumdel > int.Parse(icount.ToString()))
                        {
                            MessageBox.Show("請您輸入修理數量量具要刪除(int,<" + istocknumber + ")", "錯誤", MessageBoxButtons.OK);
                            txt數量.Focus();
                            return;
                        }
                        if (inumdel == istocknumber)//delete from database
                        {
                            DialogResult answer1 = MessageBox.Show("您確定要出入" + inumdel + "量具修理碼", "警報", MessageBoxButtons.YesNo);
                            if (answer1 == DialogResult.Yes)
                            {   string delete = tbx0;
                            //DialogResult answer2 = MessageBox.Show("Bạn có muốn xóa A trong 修理紀錄 khi trả về 5 量具 va số lượng còn lại la bằng 0, hay ko ？", "警報", MessageBoxButtons.YesNo);
                            DialogResult answer2 = MessageBox.Show("當歸還" + inumdel + "量具及存修理紀錄的量具剩余數量是0的，要不要刪除這個量具？", "警報", MessageBoxButtons.YesNo);
                                if(answer2 == DialogResult.Yes){
                                    //con.Open();
                                    string sp_delete = "sp_修理_刪除";
                                    
                                    SqlCommand sqldelete = new SqlCommand();
                                    sqldelete.Connection = con;
                                    sqldelete.CommandType = CommandType.StoredProcedure;
                                    sqldelete.CommandText = sp_delete;
                                    sqldelete.Parameters.AddWithValue("@項次", tbx0);
                                    //MessageBox.Show("刪除成功");
                                    sqldelete.ExecuteNonQuery();
                                    }
                                if (answer2 == DialogResult.No)
                                {
                                    //string delete1 = tbl6;
                                    string sp_increment = "sp_修理_更新_-n"; // tru ling yong
                                    SqlCommand sqlincrement = new SqlCommand();
                                    sqlincrement.Connection = con;
                                    sqlincrement.CommandType = CommandType.StoredProcedure;
                                    sqlincrement.CommandText = sp_increment;
                                    sqlincrement.Parameters.AddWithValue("@項次", delete);
                                    //sqlincrement.Parameters.AddWithValue("@量具狀態", delete1);
                                    sqlincrement.Parameters.AddWithValue("@n", int.Parse(txt數量.Text));
                                    sqlincrement.ExecuteNonQuery();
                                }
                                //// Sửa ngày dự định trở về kho thành ngày trở về thực tế
                                //string sp_update修理 = "sp_修理_更新";
                                //SqlCommand cmd = new SqlCommand(sp_update修理, con);
                                //cmd.CommandType = CommandType.StoredProcedure;
                                //cmd.Parameters.AddWithValue("@量具編號", txt_量具編號.Text);
                                //cmd.Parameters.AddWithValue("@返庫日期", DateTime.Parse(txt_Date返庫.Text));
                                //cmd.Parameters.AddWithValue("@修理人代號", txt_修理人.Text);
                                //cmd.Parameters.AddWithValue("@故障描述", txt_故障描述.Text.ToString());
                                //cmd.Parameters.AddWithValue("@受理人代號", txt_受理人.Text.ToString());
                                //cmd.Parameters.AddWithValue("@修理內容", txt_修理內容.Text.ToString());
                                //cmd.Parameters.AddWithValue("@備註", txt_note.Text);
                                //cmd.ExecuteNonQuery();

                                //Tra ve
                                string sp_update = "sp_量具訊息_更新_+n"; // cong vao kho
                                SqlCommand cmd1 = new SqlCommand(sp_update, con);
                                cmd1.CommandType = CommandType.StoredProcedure;
                                cmd1.Parameters.AddWithValue("@量具編號", txt_量具編號.Text);
                                //cmd1.Parameters.AddWithValue("@量具狀態", txt量具狀態.Text.ToString());
                                cmd1.Parameters.AddWithValue("@n", txt數量.Text);
                                cmd1.ExecuteNonQuery();
                                MessageBox.Show("修理成功");
                                this.Close();
                                con.Close();  
                            }
                            else
                            {
                                return;
                            }
                        }
                        else
                        {
                            //====================================================================================
                            DialogResult answer = MessageBox.Show("您確定要出入" + int.Parse(txt數量.Text) + "量具嗎？", "警報", MessageBoxButtons.YesNo);
                            if (answer == DialogResult.Yes)
                            {
                                //con.Open();
                                //tra ve
                                string delete = tbx0;
                                //string delete1 = tbl6;
                                string sp_increment = "sp_修理_更新_-n";// tru ling yong
                                SqlCommand sqlincrement = new SqlCommand();
                                sqlincrement.Connection = con;
                                sqlincrement.CommandType = CommandType.StoredProcedure;
                                sqlincrement.CommandText = sp_increment;
                                sqlincrement.Parameters.AddWithValue("@項次", delete);
                                //sqlincrement.Parameters.AddWithValue("@量具狀態", delete1);
                                sqlincrement.Parameters.AddWithValue("@n", int.Parse(txt數量.Text));
                                sqlincrement.ExecuteNonQuery();

                                //Tra ve
                                string sp_update = "sp_量具訊息_更新_+n"; // cong vao kho
                                SqlCommand cmd1 = new SqlCommand(sp_update, con);
                                cmd1.CommandType = CommandType.StoredProcedure;
                                cmd1.Parameters.AddWithValue("@量具編號", txt_量具編號.Text);
                                //cmd1.Parameters.AddWithValue("@量具狀態", txt量具狀態.Text.ToString());
                                cmd1.Parameters.AddWithValue("@n", txt數量.Text);
                                cmd1.ExecuteNonQuery();
                                MessageBox.Show("歸還成功");
                                con.Close();
                                this.Close();
                            }
                            else return;
                        }
                    }
                }
                if (xiuli == "3")
                {
                    if (txt_量具編號.Text == "")
                    {
                        MessageBox.Show("請您輸入量具編號", "錯誤", MessageBoxButtons.OK);
                        txt_量具編號.Focus();
                        return;
                    }
                    if (txt量具狀態.Text == "")
                    {
                        MessageBox.Show("請您輸入量具狀態", "錯誤", MessageBoxButtons.OK);
                        txt量具狀態.Focus();
                        return;
                    }
                    if (txt數量.Text == "")
                    {
                        MessageBox.Show("請您輸入修理數量", "錯誤", MessageBoxButtons.OK);
                        txt數量.Focus();
                        return;
                    }
                    if (txt_Date修理.Text == "")
                    {
                        MessageBox.Show("請您輸入修理日期", "錯誤", MessageBoxButtons.OK);
                        txt_Date修理.Focus();
                        return;
                    }
                    if (txt_Date返庫.Text == "")
                    {
                        MessageBox.Show("請您輸入返庫日期", "錯誤", MessageBoxButtons.OK);
                        txt_Date返庫.Focus();
                        return;
                    }

                    if (txt_修理人.Text == "")
                    {
                        MessageBox.Show("請您輸入修理人", "錯誤", MessageBoxButtons.OK);
                        txt_修理人.Focus();
                        return;
                    }

                    if (txt_受理人.Text == "")
                    {
                        MessageBox.Show("請您輸入受理人", "錯誤", MessageBoxButtons.OK);
                        txt_受理人.Focus();
                        return;
                    }
                    // kiem tra thoi gian
                    DateTime a = DateTime.Parse(txt_Date返庫.Text);
                    DateTime b = DateTime.Parse(txt_Date修理.Text);
                    if (a < b)
                    {
                        MessageBox.Show("請您輸入返庫日期 >= 修理日期", "錯誤", MessageBoxButtons.OK);
                        txt_Date返庫.Focus();
                        return;
                    }
                    // kiem tra nhap shuliang nho hon so luong trong kho
                    SqlCommand sqlcmd = new SqlCommand();
                    sqlcmd.Connection = con;
                    con.Open();
                    sqlcmd.CommandText = "select 數量 from 報廢 where 項次=" + "N'" + tbx0 + "'";
                    sqlcmd.CommandType = CommandType.Text;
                    var icount = sqlcmd.ExecuteScalar();
                    int inumdel;
                    bool k = int.TryParse(txt數量.Text, out inumdel);
                    int istocknumber = int.Parse(icount.ToString());
                    int check = int.Parse(txt數量.Text);
                    if (!k || inumdel > int.Parse(icount.ToString()))
                    {
                        MessageBox.Show("請您輸入修理數量量具要刪除(int,<" + istocknumber + ")", "錯誤", MessageBoxButtons.OK);
                        txt數量.Focus();
                        //return;
                    }
                    else if (istocknumber == 0 || check <= 0)
                    {
                        MessageBox.Show("不能修理", "錯誤", MessageBoxButtons.OK);
                    }
                    else if(inumdel == istocknumber)
                    {
                        // tien hanh inset du lieu vao bang lingyong
                        DialogResult answer = MessageBox.Show("您確定要出入" + int.Parse(txt數量.Text) + "量具嗎？", "警報", MessageBoxButtons.YesNo);
                        if (answer == DialogResult.Yes)
                        {
                            string delete = tbx0;
                            //DialogResult answer2 = MessageBox.Show("Bạn có muốn xóa A trong 修理紀錄 khi trả về 5 量具 va số lượng còn lại la bằng 0, hay ko ？", "警報", MessageBoxButtons.YesNo);
                            DialogResult answer2 = MessageBox.Show("當修理 " + inumdel + " 量具及存報廢紀錄的量具剩余數量是 0 的，要不要刪除這個量具？", "警報", MessageBoxButtons.YesNo);
                                if(answer2 == DialogResult.Yes){
                                //con.Open();
                                string sp_delete = "sp_報廢_刪除";
                                SqlCommand sqldelete = new SqlCommand();
                                sqldelete.Connection = con;
                                sqldelete.CommandType = CommandType.StoredProcedure;
                                sqldelete.CommandText = sp_delete;
                                sqldelete.Parameters.AddWithValue("@項次", delete);
                                //MessageBox.Show("刪除成功");
                                sqldelete.ExecuteNonQuery();
                                }
                                if (answer2 == DialogResult.No)
                                {
                                    //ling yong liang ju
                                    //string delete1 = tbl6;
                                    string sp_increment = "sp_報廢_更新_-n";// tru so luong san pham trong kho khi xiuli
                                    SqlCommand sqlincrement = new SqlCommand();
                                    sqlincrement.Connection = con;
                                    sqlincrement.CommandType = CommandType.StoredProcedure;
                                    sqlincrement.CommandText = sp_increment;
                                    sqlincrement.Parameters.AddWithValue("@項次", delete);
                                    //sqlincrement.Parameters.AddWithValue("@量具狀態", delete1);
                                    sqlincrement.Parameters.AddWithValue("@n", int.Parse(txt數量.Text));
                                    sqlincrement.ExecuteNonQuery();
                                }
                            //automation inset
                            string sp_insert = "sp_修理_新增";
                            SqlCommand cmd = new SqlCommand(sp_insert, con);
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.Parameters.AddWithValue("@量具編號", txt_量具編號.Text);
                            cmd.Parameters.AddWithValue("@量具狀態", txt量具狀態.Text);
                            cmd.Parameters.AddWithValue("@量具名稱", txt_Name量具.Text);
                            cmd.Parameters.AddWithValue("@規格型號", txt_規格型式.Text);
                            cmd.Parameters.AddWithValue("@存放地點", txt_存放地點.Text.ToString());
                            cmd.Parameters.AddWithValue("@測量範圍", txt_測量範圍.Text.ToString());
                            cmd.Parameters.AddWithValue("@數量", Int32.Parse(txt數量.Text));
                            cmd.Parameters.AddWithValue("@修理日期", DateTime.Parse(txt_Date修理.Text));
                            cmd.Parameters.AddWithValue("@返庫日期", DateTime.Parse(txt_Date返庫.Text));
                            //cmd.Parameters.AddWithValue("@領用人代號", txt_領用部門.Text);
                            cmd.Parameters.AddWithValue("@修理人代號", txt_修理人.Text);
                            cmd.Parameters.AddWithValue("@故障描述", txt_故障描述.Text.ToString());
                            cmd.Parameters.AddWithValue("@受理人代號", txt_受理人.Text.ToString());
                            cmd.Parameters.AddWithValue("@修理內容", txt_修理內容.Text.ToString());
                            cmd.Parameters.AddWithValue("@備註", txt_note.Text);
                            cmd.ExecuteNonQuery();

                            //string dele = "Select 數量 from 領用";

                            MessageBox.Show("修理成功");
                            con.Close();
                            this.Close();
                            frmxiulijilu frm = new frmxiulijilu(b量具Search, b量具Insert, b量具Edit, b量具Del);
                            frm.ShowDialog();
                            this.Close();
                        }
                        else return;
                    }
                    else
                    {
                        // tien hanh inset du lieu vao bang xiuli
                        DialogResult answer = MessageBox.Show("您確定要出入" + int.Parse(txt數量.Text) + "量具嗎？", "警報", MessageBoxButtons.YesNo);
                        if (answer == DialogResult.Yes)
                        {
                            // - baofei liang ju
                            string delete = tbx0;
                            //string delete1 = tbl6;
                            string sp_increment = "sp_報廢_更新_-n";// tru so luong san pham trong kho khi xiuli
                            SqlCommand sqlincrement = new SqlCommand();
                            sqlincrement.Connection = con;
                            sqlincrement.CommandType = CommandType.StoredProcedure;
                            sqlincrement.CommandText = sp_increment;
                            sqlincrement.Parameters.AddWithValue("@項次", delete);
                            //sqlincrement.Parameters.AddWithValue("@量具狀態", delete1);
                            sqlincrement.Parameters.AddWithValue("@n", int.Parse(txt數量.Text));
                            sqlincrement.ExecuteNonQuery();

                            //automation inset
                            string sp_insert = "sp_修理_新增";
                            SqlCommand cmd = new SqlCommand(sp_insert, con);
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.Parameters.AddWithValue("@量具編號", txt_量具編號.Text);
                            cmd.Parameters.AddWithValue("@量具狀態", txt量具狀態.Text);
                            cmd.Parameters.AddWithValue("@量具名稱", txt_Name量具.Text);
                            cmd.Parameters.AddWithValue("@規格型號", txt_規格型式.Text);
                            cmd.Parameters.AddWithValue("@存放地點", txt_存放地點.Text.ToString());
                            cmd.Parameters.AddWithValue("@測量範圍", txt_測量範圍.Text.ToString());
                            cmd.Parameters.AddWithValue("@數量", Int32.Parse(txt數量.Text));
                            cmd.Parameters.AddWithValue("@修理日期", DateTime.Parse(txt_Date修理.Text));
                            cmd.Parameters.AddWithValue("@返庫日期", DateTime.Parse(txt_Date返庫.Text));
                            //cmd.Parameters.AddWithValue("@領用人代號", txt_領用部門.Text);
                            cmd.Parameters.AddWithValue("@修理人代號", txt_修理人.Text);
                            cmd.Parameters.AddWithValue("@故障描述", txt_故障描述.Text.ToString());
                            cmd.Parameters.AddWithValue("@受理人代號", txt_受理人.Text.ToString());
                            cmd.Parameters.AddWithValue("@修理內容", txt_修理內容.Text.ToString());
                            cmd.Parameters.AddWithValue("@備註", txt_note.Text);
                            cmd.ExecuteNonQuery();

                            //string dele = "Select 數量 from 領用";

                            MessageBox.Show("修理成功");
                            con.Close();
                            this.Close();
                            frmxiulijilu frm = new frmxiulijilu(b量具Search, b量具Insert, b量具Edit, b量具Del);
                            frm.ShowDialog();
                            this.Close();
                        }
                        else return;
                    }
                }
            }
            catch (Exception ex) { con.Close(); MessageBox.Show(ex.Message); }
            finally { con.Close(); }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn修理紀錄_Click(object sender, EventArgs e)
        {
            frmxiulijilu frm = new frmxiulijilu(b量具Search, b量具Insert, b量具Edit, b量具Del);
            frm.ShowDialog();
        }

        private void txt_量具編號_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 量具訊息 where 量具編號=N'{0}'", txt_量具編號.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName量具編號.Text = "";
                else lbName量具編號.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void txt_存放地點_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 存放地點訊息 where 存放地點代號=N'{0}'", txt_存放地點.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName存放地點.Text = "";
                else lbName存放地點.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void txt_修理人_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 修理人 where 修理人代號=N'{0}'", txt_修理人.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName修理人.Text = "";
                else lbName修理人.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void txt_受理人_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 修理_受理人 where 受理人代號=N'{0}'", txt_受理人.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                //int icount=0;
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbName受理人.Text = "";
                else lbName受理人.Text = temp;
                con.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btnSearch受理人_Click(object sender, EventArgs e)
        {
            frmXiuliRen frm = new frmXiuliRen(txt_修理人);
            frm.ShowDialog();
        }

        private void btn請購人員Search_Click(object sender, EventArgs e)
        {
            frmShouliRen frm = new frmShouliRen(txt_受理人);
            frm.ShowDialog();
        }

        private void txt數量_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) || e.KeyChar == '.')
            {
                e.Handled = true;
                MessageBox.Show("請您輸入號碼", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }    
    }
}
